//
//  MorePicVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/17.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MorePicVC.h"

@interface MorePicVC ()<UIScrollViewDelegate>

{
    UIImageView *_img;
    UITapGestureRecognizer *_tap;
    UILongPressGestureRecognizer *_longPress;
}

@property (nonatomic, strong) UIControl *control;

@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *view;







@end

@implementation MorePicVC


-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.control = [[UIControl alloc]initWithFrame:self.view.frame];
    self.control.backgroundColor = [UIColor clearColor];
    self.control.hidden = YES;
    [self.control addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.control];
    

    


        
    NSString *str = [self.getPicImage substringToIndex:[self.getPicImage length] - 1];
    self.array = [str componentsSeparatedByString:@","];
//    NSLog(@"%@",self.array);

    
    
        
        [self creatScrollView];


    
    
    
    
    
    
    
    // Do any additional setup after loading the view.
}




-(void)back
{
    [self dismissViewControllerAnimated:nil completion:nil];
}




-(void)creatScrollView
{
    self.scrollView = [[UIScrollView alloc]initWithFrame:self.view.frame];
    self.scrollView.backgroundColor = [UIColor blackColor];
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.array.count, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    
    for (int i = 0; i < self.array.count; i ++) {
        
        UIImageView *img = [[UIImageView alloc]init];
        [self.scrollView addSubview:img];
        
        // 获取 图片的size
        [img sd_setImageWithURL:[NSURL URLWithString:self.array[i]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            img.image = image;
            img.frame = CGRectMake(i * WIDTH, HEIGHT / 2 - (image.size.height/4), WIDTH, image.size.height / 2);
        }];
        
        
        
        UILabel * noLabel = [[UILabel alloc]initWithFrame:CGRectMake(i * WIDTH, H * 50, WIDTH, H *20)];
        [_scrollView addSubview: noLabel];
        [noLabel setBackgroundColor:[UIColor blackColor]];
        noLabel.textAlignment = NSTextAlignmentCenter;
        noLabel.text = [NSString stringWithFormat:@"%d / %ld",i+1,_array.count];
        noLabel.textColor = [UIColor whiteColor];
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        view.backgroundColor = [UIColor clearColor];
        [self.scrollView addSubview:view];
        [view addGestureRecognizer:tap];
     
        
/*
        //        加个线程
                static dispatch_once_t onceToken;
                dispatch_once(&onceToken, ^{
        
                    // 获得 image图片
        
                    NSData *data = [NSData dataWithContentsOfURL:imageUrl];
        
        
                });
        
        
                NSURL *imageUrl = [NSURL URLWithString:self.array[i]];
                NSURLRequest *request = [NSURLRequest requestWithURL:imageUrl];
                [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                    UIImage *image = [UIImage imageWithData:data];
        
                    img.image = image;
        
                    img.frame = CGRectMake(i * WIDTH, HEIGHT / 2 - (image.size.height/4), WIDTH, self.size.height / 2);
                }];
        */
        
/*
         // 长摁保存
         //打开用户交互
         _img.userInteractionEnabled = YES;
         //创建一个长按的手势
         UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
         //图片上加手势
         [view addGestureRecognizer:longPress];
         //设置手势触发最短时间
         _longPress.minimumPressDuration = 2;
         
         
         //           双击手势确定监测失败才会触发单击手势的相应操作
         [tap requireGestureRecognizerToFail:longPress];
         
         
         */
        
    }
}


-(void)tapClick
{
    [self dismissViewControllerAnimated:nil completion:nil];
}




//-(void)longAction:(UILongPressGestureRecognizer *)longPress
//{
//    //长按触发保存图片到手机相册的操作
//    if (longPress.state == UIGestureRecognizerStateBegan){
//        
//        UIImageWriteToSavedPhotosAlbum(_img.image, nil, nil, nil);
//        NSLog(@"aaaa");
//    }
//        
//    
//
//}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
